layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);

    //上传缩略图
    upload.render({
        elem: '.thumbBox',
        url: '../../json/userface.json',
        method : "get",  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        done: function(res, index, upload){
            var num = parseInt(4*Math.random());  //生成0-4的随机数，随机显示一个头像信息
            $('.thumbImg').attr('src',res.data[num].src);
            $('.thumbBox').css("background","#fff");
        }
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger : "click",
        done : function(value, date, endDate){
            submitTime = value;
        }
    });
    form.on("radio(type)",function(data){
        var typeId=data.value;
        
    });

    form.verify({
        title : function(val){
            if(val == ''){
                return "标题不能为空";
            }
        },
        brief : function(val){
            if(val == ''){
                return "简介不能为空";
            }
        },
        type : function(val){
            if(val == ''){
                return "请选择资讯类型";
            }
        },
//        content : function(val){
//            if(val == ''){
//                return "内容不能为空";
//            }
//        }
    })
    form.on("submit(addNews)",function(data){
    	
        var typeId=data.field.typeId;
        
        if(!typeId){
        	layer.msg('请选择资讯类型');
        	return;
        }
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        
        $.ajax({
        	type:'post',
        	data:data.field,
        	url:'action',
        	dataType: "json", 
        	success:function(res){
        		console.log(res.code);
        		if(res.code==200){
        			top.layer.close(index);
                    top.layer.msg("文章添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
        		}else{
        			top.layer.close(index);
                    top.layer.msg("文章添加失败！");
        		}
        	},
        	fail:function(){
        		top.layer.close(index);
                top.layer.msg("文章添加失败！");
        	}
        })        
        
    })

    

    //创建一个编辑器
    var editIndex = layedit.build('content',{
        height : 535,
        uploadImage : {
            url : "../../json/newsImg.json"
        }
    });

})